<?php
	//积分换话费接口－points_to_fee.php
	//create by L
	//http://42.121.57.193:7788/platform/points_to_fee.php?phone=13055147797&points=4

	include_once "init.php";

	//账户手机号码
	$phone=$_GET["phone"];
	//要兑换的积分数额
	$points=$_GET["points"];
	if('' == $phone )
	{
		die("-1|手机号码不可以为空");
	}
	if (strlen($phone) != 11 || false==is_numeric($phone) || ord($phone)!=49)
	{
		die("-1|手机号码必须是11位数字，并且以1开头");
	}
	if('' == $points)
	{
		die("-1|金额不可以为空");
	}

	$con    = db_connect(DB_NAME);
	$res = mysql_query("SELECT points,uuid from user where phone='$phone'") or die("-1|查询数据库失败");
	$row = mysql_fetch_row($res);

	if (empty($row))
	{
		mysql_close($con);
		die("-1|手机号码未注册");
	}
	$db_points = $row['0'];

	if ($points > $db_points)
	{
		die("-1|兑换积分超出当前积分");
	}
	else
	{
		//计算兑换后积分的当前积分
		$current_points = $db_points - $points;
	}


	//更新积分
	$res = mysql_query("UPDATE user SET points='$current_points' where phone='$phone'") or die("-1|查询数据库失败");
	mysql_close($con);

	if ( $res === false )
	{
		mysql_close($con);
		die("-1|修改积分失败");
	}
	mysql_close($con);
	//兑换积分比例--1积分：0.01元
	$rate 	= 0.001;
	$money 	= $points * $rate;


	//修改余额
	$account     = 'a_'.$row['1'];
	$loginName	   = VOS_USER;
	$loginPassword = VOS_PW;
	$result = pay($loginName,$loginPassword,$account,$money);
	if('0'!=$result[0]&&'|'!=$result[1])
	{
		die("-1|积分兑换充值失败");
	}
	else
	{
		echo '0|'.$money;
	}


?>